package com.zx.lean_ui.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.zx.lean_ui.R;
import com.zx.lean_ui.fragment.BlankFragment;
import com.zx.lean_ui.fragment.BlankFragment3;
import com.zx.lean_ui.fragment.ItemFragment;
import com.zx.lean_ui.interfaces.IFragmentCallback;
import com.zx.lean_ui.util.Config;

public class MainActivity3 extends AppCompatActivity implements View.OnClickListener {

    private Button btn;
    private Button btn2;
    private FrameLayout framelayout;
    BlankFragment3 bf;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        initView();
    }

    private void initView() {
        btn = findViewById(R.id.btn);
        btn2 = findViewById(R.id.btn_2);
        framelayout = findViewById(R.id.framelayout);
        btn.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn:
                Bundle bundle=new Bundle();
                bundle.putString("message","我喜欢享学课堂");
                bf=new BlankFragment3();
                bf.setArguments(bundle);
                bf.setFragmentCallback(new IFragmentCallback() {
                    @Override
                    public void sendMsgToActivity(String msg) {
                        Config.d("BlankFragment3","MainActivity3. bf.setFragmentCallback:msg======="+msg);
                    }

                    @Override
                    public String getMsgFromActivity(String msg) {
                        Config.d("BlankFragment3","MainActivity3. bf.getMsgFromActivity:msg======="+msg);
                        return "这是Activity";
                    }
                });
                replaceFragment(bf);
                break;
            case R.id.btn_2:
                replaceFragment(new ItemFragment());
                break;


        }
    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager manager=getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.framelayout,fragment);
        //将fragment添加到栈里
        transaction.addToBackStack(null);

        transaction.commit();
    }
}